package com.netease.xone.itemview;

import protocol.meta.AppHead;
import protocol.meta.AppInfo;
import protocol.meta.AppInfoDetail;
import Utils.StackTraceUtil;
import Utils.TimeFormatUtil;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.netease.log.NTLog;
import com.netease.xone.R;
import com.netease.xone.activity.ActivitySourceDetail;
import com.netease.xone.view.ProfileView;


public class RenderInfoHead implements IRenderInfoBase, OnClickListener{

    public static final String TAG = RenderInfoHead.class.getSimpleName();

    private View mRoot;
    private ProfileView mAvatar;
    private ImageView mAvatarLabel;
    private TextView mNickname;
    private TextView mTime;
    private String mInfoId;
    private AppHead mAppHead;
    
    public RenderInfoHead(View v) {
        mRoot = v;
        mAvatar = (ProfileView)v.findViewById(R.id.avatar);
        mAvatarLabel = (ImageView)v.findViewById(R.id.avatar_label);
        mNickname = (TextView)v.findViewById(R.id.nickname);
        mTime = (TextView)v.findViewById(R.id.time);
        
        mRoot.setOnClickListener(this);
    }

    @Override
    public void renderView(Context context, AppInfo appInfo) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        renderView(context, appInfo.getUser(), appInfo.getTime());
        mInfoId = appInfo.getInfoId();
    }

    @Override
    public void renderView(Context context, AppInfoDetail appInfoDetail) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        renderView(context, appInfoDetail.getUser(), appInfoDetail.getTime());
        mInfoId = appInfoDetail.getInfoId();
    }
    
    private void renderView(Context context, AppHead head, long time) {
        String value = null;
        
        if (null != head) {
            value = head.getAvatar();
            if (TextUtils.isEmpty(value)) {
            } else {
                mAvatar.setImageUrl(head.getUserAttr(), value);
            }
            
            mAvatarLabel.setImageDrawable(null);
            switch (head.getUserType()) {
                case AppHead.USER_TYPE_OFFICIAL: {
                    switch (head.getUserAttr()) {
                        case AppHead.USER_ATTR_GAME:
                            mAvatarLabel.setImageResource(R.drawable.icon_label_game);
                            break;
                        case AppHead.USER_ATTR_COMPOSITE:
                            mAvatarLabel.setImageResource(R.drawable.icon_label_other);
                            break;
                    }
                    break;
                }
                default:
                    break;
            }
            
            value = head.getNickname();
            mNickname.setText(value);
        }
        
        value = TimeFormatUtil.getTimeFormat(context, time);
        mTime.setText(value);
        
        mAppHead = head;
    }
    
    @Override
    public void onClick(View v) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        switch (mAppHead.getUserType()) {
            case AppHead.USER_TYPE_USER: {
                // TODO 跳转到个人主页
                break;
            }
            case AppHead.USER_TYPE_OFFICIAL: {
                ActivitySourceDetail.startActivity(v.getContext(), Integer.valueOf(mAppHead.getUserId()), mAppHead.getNickname());
                break;
            }
        }
    }
}
